/*
 * TMap Designer - A Thematic Map design software
 * Copyright (C) 2006 World Health Organization
 * http://www.who.int
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */
package who.tmap.toolkit.shape.annotation.impl;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;

import org.apache.log4j.Logger;

import who.tmap.toolkit.shape.annotation.LineShape;

/**
 * @author stl
 *
 * To change the template for this generated type comment go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
public class LineShapeImpl extends BaseAnnotationGraphicShapeImpl implements LineShape {

    private static Logger logger = Logger.getLogger(LineShapeImpl.class);     
    
	private Point2D p1 =null, p2 =null;

	public LineShapeImpl() {
		setType( LINE );
	}

	/**
	 * @return
	 */
	public Point2D getP1() {
		return p1;
	}

	/**
	 * @return
	 */
	public Point2D getP2() {
		return p2;
	}

	/**
	 * @param point
	 */
	public void setP1(Point2D point) {
		p1 = point;
	}

	/**
	 * @param point
	 */
	public void setP2(Point2D point) {
		p2 = point;
	}

	public void draw(Graphics g) {
		Graphics2D g2D =(Graphics2D)g;
		
		g.setColor( getColor() );
		
		Line2D line =new Line2D.Double( p1, p2 );
	
		g2D.draw( line );
		
		bounds =line.getBounds2D();
		
		if ( isSelected() ) {	            
			displayHandleRectangle( g2D, p1.getX(), p1.getY() );
			displayHandleRectangle( g2D, p2.getX(), p2.getY() );
		}
	}
	
	/*
	 * Empty method as there is nothing to fill for a line !
	 */
	public void fill( Graphics g ) {
	}
	
	public void fill( Graphics g, Color color ) {
	}
	
	public boolean contains( Point2D p ) {
	    return new Line2D.Double( p1, p2 ).intersects( getHandleRectangle( p.getX(), p.getY() ) );
	}
	
	public boolean isInside( Rectangle2D r ) {	
		Rectangle2D rect =new Rectangle2D.Double();
		rect.setFrameFromDiagonal( p1, p2 );
		return r.contains( rect );
	}	
	
	public int getHandleIndexAt( Point2D p ) {
		int handle =-1;
		
		if ( getHandleRectangle( p1.getX(), p1.getY() ).contains( p ) ) {
			handle =0;
		} else if ( getHandleRectangle( p2.getX(), p2.getY() ).contains( p ) ) {
			handle =1; 
		}
		
		logger.debug( "handle found=" + handle );
		
		return handle;
	}
	
	public void updateShape(int idx, Point2D p) {
		switch( idx ) {
			case 0:
				p1 =p;
				break;
			case 1:
				p2 =p;
				break;
		}
	}
	
	public void moveShape( Point2D anchor, Point2D movingAnchor ) {
	    
	    double offsetX =movingAnchor.getX() - anchor.getX();
	    double offsetY =movingAnchor.getY() - anchor.getY();
	    
	    p1.setLocation( p1.getX() + offsetX, p1.getY() + offsetY );
	    p2.setLocation( p2.getX() + offsetX, p2.getY() + offsetY );
	    
	}	
	
	public String toString() {
		String str =p1 != null ? "P1.x=" + p1.getX() + ", P1.y=" + p1.getY() : "P1=null";
		str+=p2 != null ? ", P2.x=" + p2.getX() + ", P2.y=" + p2.getY() : ", P2=null";
		
		return str;
	}

}
